package com.example.system.service.impl;

import com.example.core.enums.ResultCode;
import com.example.security.exception.ServiceException;
import com.example.system.entity.DO.UserDO;
import com.example.system.entity.DTO.UserQueryDTO;
import com.example.system.entity.DTO.UserStatusDTO;
import com.example.system.entity.VO.UserVO;
import com.example.core.enums.UserStatus;
import com.example.system.mapper.UserMapper;
import com.example.system.service.IUserService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<UserVO> list(UserQueryDTO userQueryDTO) {
        PageHelper.startPage(userQueryDTO.getPageNum(), userQueryDTO.getPageSize());

        List<UserVO> userVOS = userMapper.selectUserList(userQueryDTO);
        return userVOS;
    }

    @Override
    public int updateStatus(UserStatusDTO userStatusDTO) {
        UserDO user = userMapper.selectById(userStatusDTO.getUserId());
        if (user == null) {
            throw new ServiceException(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        if (!UserStatus.checkStatus(userStatusDTO.getStatus())) {
            throw new ServiceException(ResultCode.FAILED);
        }

        user.setStatus(userStatusDTO.getStatus());
        return userMapper.updateById(user);
    }
}
